// src/stores/auth.js (Pinia)
import { defineStore } from 'pinia';
import { encrypt, decrypt } from '@/utils/crypto';

export const useAuthStore = defineStore('auth', {
  state: () => ({
    user: null,     // 用户信息
    token: null     // 从HttpOnly Cookie获取
  }),
  actions: {
    // 登录成功后缓存数据
    loginSuccess(userData) {
      this.user = userData;
      
      // 非敏感信息加密存储
      localStorage.setItem('user', encrypt(JSON.stringify(userData)));
    },
    // 初始化时从存储加载
    loadFromCache() {
      const cachedUser = localStorage.getItem('user');
      if (cachedUser) {
        this.user = JSON.parse(decrypt(cachedUser));
      }
    },
    // 退出登录清理
    logout() {
      this.user = null;
      localStorage.removeItem('user');
      // 调用后端接口清除HttpOnly Cookie
    }
  }
});